<?php

require_once(APPLICATION_PATH . "/../library/Exts/Services/Facebook.php");

class Exts_Services_Facebook_Abstract {
	const GRAPH_URL = 'http://graph.facebook.com/?id=';
	//const URL_ME = 'https://graph.facebook.com/142008882519161/feed';
	const URL_ME = 'https://graph.facebook.com/me/feed';

    const APP_ID = '124950474237644';
    const API_KEY = '4407cb27592ab4df9d108f3d69582c44';
    const SECRET = 'af843c2866b392d7fca944691022506c';
    //const URL = BASE_URL;
    const USE_COOKIE = TRUE;
    const THROW_EXCEPTION = TRUE;
    const DISABLE_SSL_CHECK = FALSE;
    const DECODE_JSON = TRUE;
    const ALLOW_FILE_UPLOADS = FALSE;
    const CURL_TIMEOUT = 1000;
    const CURL_OPT_CONNECTTIMEOUT = 1000;
	//const CA_CERT_PATH = CURL_CA_CERT_PATH;


	protected $_token;
	
	public function __construct() {

	}

	public function setToken($token) {
		$this->_token = $token;
	}

	public function getToken() {
		return $this->_token;
	}

	/**
	 *
	 * @param <type> $url
	 * @return <type>
	 */
	public function getPageInfo($url) {

		try {
			$response = file_get_contents(self::GRAPH_URL . $url);

			$results = Zend_Json::decode($response);
			return $results;
		} catch (Exception $e) {
			echo $e->getMessage() . '<br/>';
			return array();
		}
	}

	public function send($url, $params) {

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		curl_exec($ch);
		$info = curl_getinfo($ch);
		var_dump($info);
		echo "<br/>--<br/>";
		var_dump(curl_errno($ch));
		curl_close($ch);
		exit;
	}

	public function stream($link) {
		try {
			$appId = '124950474237644';
			$apiKey = '4407cb27592ab4df9d108f3d69582c44';
			$apiSecret = 'af843c2866b392d7fca944691022506c';
			$facebook = new Facebook($apiKey,$apiSecret);
			/* $fb = new FacebookRestClient($apiKey, $apiSecret);
			  $testtoken= "<Your Temporary Token>";
			  $result = $fb->call_method('facebook.auth.getSession',
			  array('auth_token' => $testtoken, 'generate_session_secret' => true));
			 */
			try {
				$status = $facebook->api_client->users_setStatus('probando 1234', 'me');
			} catch (Exception $o) {
				print_r($o);
			}


			try {
				$facebook->api_client->stream_publish($link, null, null, '142008882519161');
				echo "PUBLICO";
				exit;
			} catch (Exception $o) {
				print_r($o);
				exit;
			}

			//$fb->api_client->stream_publish($status, null, null,  $item['pageid']);

			/* $session_key = $result['session_key'];
			  $facebook = new Facebook($apiKey, $apiSecret);
			  //$facebook->api_client->session_key = FB_SESSION;
			  $facebook->api_client->expires = 0;
			  $message = ''; */
			/*
			  $attachment = array(
			  'name' => $_POST["name"],
			  'href' => $_POST["href"],
			  'description' => $_POST["description"],
			  'media' => array(array('type' => 'image',
			  'src' => $_POST["src"],
			  'href' => $_POST["href"])));

			  //$action_links = array(array('text' => 'Visit Us', 'href' => '<link to some place here>'));
			  $action_links = array(array('href' => $link ));

			  //$attachment = json_encode($attachment);
			  $action_links = json_encode($action_links);

			  $target_id = "<Target Id>";
			  //			$session_key = FB_SESSION;

			  if ($facebook->api_client->stream_publish($message, '', $action_links, null, $target_id)) {
			  echo "Added on FB Wall";

			  }
			 */
		} catch (Exception $e) {
			echo $e . "<br />";
		}

		exit;
	}

}